package com.cxb.demotest.misc;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;

/**
 * @author Xbao
 * @description
 * @date 2022/7/14 16:56
 */
public class BloomTest {

  private static int size = 1000000;//预计要插入多少数据，即布隆数组长度
  private static double fpp = 0.01;//期望的误判率

  // private static BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), size, fpp);
  private static BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), size);

  public static void main(String[] args) {
    //插入数据
    for (int i = 0; i < 1000000; i++) {
      bloomFilter.put(i);
    }
    double count = 0;
    for (int i = 1000000; i < 2000000; i++) {
      if (bloomFilter.mightContain(i)) {
        count++;
        // System.out.println(i + "误判了");
      }
    }
    System.out.println("100w条数据,其中总共的误判数:" + count + "误判率 :"+ (count/1000000L) * 100 + "%");
    // 如果增加过滤器哈希数组长度为100 0000，则默认误判率会降低到 0.003%
  }
}
